package org.example.maven.museumuser.service.impl;

import com.fasterxml.jackson.core.JsonProcessingException;
import org.example.maven.museumuser.mapper.ExhibitionMapper;
import org.example.maven.museumuser.domain.pojo.exhibition.ExhibitionList;
import org.example.maven.museumuser.domain.pojo.exhibition.ExhibitionTicket;
import org.example.maven.museumuser.domain.pojo.exhibition.UserTicketOrder;
import org.example.maven.museumuser.service.ExhibitionOrderService;
import org.example.maven.museumuser.service.ExhibitionService;
import org.example.maven.museumuser.utils.JwtUtils;
import org.example.maven.museumuser.utils.ThreadlocalUtils;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import static org.example.maven.museumuser.domain.pojo.StaticVariable.*;

@Service
public class ExhibitionServiceImpl implements ExhibitionService {
    @Autowired
    private ExhibitionMapper exhibitionMapper;
    @Autowired
    private RedissonClient redissonClient;
    @Autowired
    private ExhibitionOrderService executeOrderTransaction;


    @Override
    public List<ExhibitionList> getAllExhibition() {
        return exhibitionMapper.getAllExhibition();
    }

    @Override
    public ExhibitionList getExhibitionById(Integer id) {
        return exhibitionMapper.getExhibitionById(id);
    }

    @Override
    public List<Map<String, String>> getExhibitionByDate(LocalDate startDate) {
        List<String> exhibitionByDate =exhibitionMapper.getExhibitionByDate(startDate);
        return exhibitionByDate.stream().map(name -> Map.of("name", name)).toList();
    }

    @Override
    public List<ExhibitionTicket> getExhibitionByName(String name,LocalDate startDate) {
        System.out.println("1"+exhibitionMapper.getExhibitionByName(name,startDate));
        List<ExhibitionTicket> exhibitionTickets = new ArrayList<>();
        exhibitionTickets.add(exhibitionMapper.getExhibitionByName(name,startDate));
        return exhibitionTickets;
    }

    @Override
    public ExhibitionList getExhibitionDetailByName(String name, LocalDate startDate) {
        return exhibitionMapper.getExhibitionDetailByName(name,startDate);
    }

    @Override
    public String submitExhibitionReservation(UserTicketOrder userTicketOrder) throws JsonProcessingException {
        String phone=JwtUtils.getJwtUsername(ThreadlocalUtils.get());
        //获取锁,一个用户只允许下一单
        RLock lock = redissonClient.getLock(Lock_Order_Key+phone);
        //获取锁
        if(!lock.tryLock()){
            //获取锁失败
            return "请勿重复提交";
        }
        try{
            //判断该用户是否已购买
            if(exhibitionMapper.getExhibitionTicketById(userTicketOrder.getTicketId(),userTicketOrder.getIdentity())>0){
                return "该用户已购买";
            }
            // 3. 执行库存检查和下单（在同一个事务中）
            return executeOrderTransaction.executeOrderTransaction(userTicketOrder);
        }finally {
            //释放锁
            lock.unlock();
        }
    }

}
